<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-03-24 18:49:03
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
class Page
{
    public $size;
    public $total;
    public $pagenum;
    public $curpage;
    public $url;
    public function __construct($total, $size = 50, $url, $hash = '')
    {
        global $_G;
        $this->total   = $total;
        $this->size    = $size;
        $this->pagenum = ($this->total == 0) ? 0 : ceil($this->total / $this->size);
        $this->url     = $url;
        $this->curpage = isset($_GET['p']) && intval($_GET['p']) > 0 ? intval($_GET['p']) : 1;
    }
    public function getStart()
    {
        return ($this->curpage - 1) * $this->size;
    }
    public function getSize()
    {
        return $this->size;
    }
    public function show()
    {
        $pageshow = $option = '';
        if($this->pagenum>9){
            $option = '<select class="humppage z" onchange="location.href=this.value">';
            for ($i = 1; $i <= $this->pagenum; $i++) {
                $option .= '<option '.($this->curpage == $i?'selected':'').' value="./'.$this->url . '&p=' . $i . $hash.'">'.$i.'</option>';
            }
            $option .= '</select>';
        }
        for ($i = 1; $i <= $this->pagenum; $i++) {
            if ($i < 4 || ($i > $this->pagenum - 2) || ($i < $this->curpage + 2 && $i > $this->curpage - 2)) {
                if ($this->curpage == $i) {
                    $pageshow .= $option?$option:'<strong class="page-active">' . $i . '</strong>';
                } else {
                    $pageshow .= '<a class="" href="./' . $this->url . '&p=' . $i . $hash . '">' . $i . '</a>';
                }
            } else if ($i == $this->pagenum - 2) {
                $pageshow .= '<strong>....</strong>';
            } else if ($i == 4 && $this->curpage >= 6 && $this->curpage < $this->pagenum) {
                $pageshow .= '<strong>....</strong>';
            }
        }
        
        return $pageshow;
    }
    public function showweui()
    {
        $pre  = '<div class="weui-flex__item"><a class="weui-btn weui-btn_default"  href="./' . $this->url . '&p=' . ($this->curpage - 1) . '">' . lang('plugin/jameson_read', 'shangye') . '</a></div>';
        $next = '<div class="weui-flex__item"><a  class="weui-btn weui-btn_default" href="./' . $this->url . '&p=' . ($this->curpage + 1) . '">' . lang('plugin/jameson_read', 'xiaye') . '</a></div>';
        if ($this->curpage == 1) {
            $pre = '';
        }
        if ($this->curpage >= $this->pagenum) {
            $next = '';
        }
        return $pre . $next;
    }
}